package com.green.javagreen.util;


import java.util.ArrayList;
import java.util.List;

/**
 * email:brioal@foxmail.com
 * github:https://github.com/Brioal
 * Created by brioa on 2018/1/11.
 */

public class TextUtil {

    /**
     * 根据分号进行分割,去除空白
     *
     * @param string
     * @return
     */
    public static String[] splitToArray(String string) {
        return splitToArray(string, ";");
    }

    /**
     * 根据分号进行分割,去除空白
     *
     * @param string
     * @return
     */
    public static String[] splitToArray(String string, String ch) {
        if (TextUtil.isNotValid(string)) {
            return null;
        }
        String[] array = string.split(ch);
        List<String> list = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            String str = array[i];
            str = str.trim();
            if (isValid(str)) {
                list.add(str);
            }
        }
        String[] strings = new String[list.size()];
        return list.toArray(strings);
    }

    /**
     * 判断字符串是否可用
     *
     * @param str
     * @return
     */
    public static String getValue(String str) {
        if (str == null) {
            return "";
        }
        return str;
    }

    /**
     * 判断字符串是否可用
     *
     * @param str
     * @return
     */
    public static boolean isStringAvailableAddNotNull(String str) {
        if (str == null) {
            return false;
        }
        if (str.equals("")) {
            return false;
        }
        if (str.equals("null")) {
            return false;
        }
        return true;
    }

    /**
     * 判断字符串是否可用
     *
     * @param str
     * @return
     */
    public static boolean isStringAvailable(String str) {
        if (str == null) {
            return false;
        }
        if (str.equals("")) {
            return false;
        }
        return true;
    }

    /**
     * 判断字符串是否错误
     *
     * @param str
     * @return
     */
    public static boolean isStringError(String str) {
        if (str == null) {
            return true;
        }
        if (str.equals("")) {
            return true;
        }
        return false;
    }


    /**
     * 是否全是英文
     *
     * @param str
     * @return
     */
    public boolean isAllEn(String str) {
        if (!isStringAvailable(str)) {
            return false;
        }
        return str.matches("[a-zA-Z]+");
    }

    /**
     * 是否全是中文
     *
     * @param str
     * @return
     */
    public boolean isAllChinese(String str) {
        if (!isStringAvailable(str)) {
            return false;
        }
        char[] chs = str.toCharArray();
        for (char ch : chs) {
            if (!isChinese(ch)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 是否是中文字符
     *
     * @param c
     * @return
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }
        return false;
    }


    /**
     * 判断字符串是否不可用
     *
     * @param str
     * @return
     */
    public static boolean isNotValid(String str) {
        if (str == null) {
            return true;
        }
        if (str.equals("")) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串是否不可用
     *
     * @param str
     * @return
     */
    public static boolean isValid(String str) {
        if (str == null) {
            return false;
        }
        if (str.equals("")) {
            return false;
        }
        return true;
    }


}
